/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2025-03-12 17:09:03
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-19 18:36:32
 */

const app = getApp()
import {
  showMsg
} from '../../utils/config.js'
import {
  reqOrgList
} from '../../request/api'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: false,
    search: false,
    switchTab: false,
    isImmediateRequest: false,
    orgInfo: {}
  },
  options: {
    addGlobalClass: true
  },

  data: {
    dataList: [],
    page: 1,
    isNextPage: true,
    triggered: false, //是否下拉刷新列表
    timer: null,

    orgItemInfo: {},

  },

  observers: {
    async isImmediateRequest(val) {
      if (val) {
        await this.getDataFirst()
      }
    },
    show(val) {
      if (!val) return
      if (!(this.data.dataList && this.data.dataList.length)) {
        this.getDataList()
      }
      if (this.data.isImmediateRequest && this.data.orgInfo.orgId) {
        console.log('orgInfo', this.data.orgInfo);
        this.setData({
          orgItemInfo: this.data.orgInfo
        })
      }

    }
  },
  async attached() {
    console.log('this.data.isImmediateRequest', this.data.isImmediateRequest);
    if (this.data.isImmediateRequest) {
      await this.getDataFirst()
    }

  },
  detached() {
    console.log('组件实例从页面节点树中移除');
  },


  methods: {
    // 获取组织列表
    async getDataList() {
      let params = {
        tenantId: app.globalData.tenantId,
        userId: app.globalData.userId,
        roleName: app.globalData.rolename,
        type: "4",
        orgName: this.data.orgName,
      }
      const res = await reqOrgList(params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data

      this.setData({
        dataList: rows,
      })

    },
    async getInitDataList() {
      this.setData({
        dataList: [],
        page: 1,
        isNextPage: true,
      })
      await this.getDataList()
    },

    // 获取第一条  -- 页面必填单位ID
    async getDataFirst() {
      await this.getDataList()
      const orgItemInfo = this.data.dataList[0]
      this.triggerEvent('confirmUnitSelection', {
        showUnitSelection: false,
        msg: true,
        orgInfo: orgItemInfo
      });
    },
    // 滚动到底部触发
    lower() {
      return
      if (!this.data.isNextPage) return
      this.setData({
        page: this.data.page + 1,
      })
      this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
      await this.getInitDataList()
      this.setData({
        triggered: false,
      })
    },

    // 输入框搜索
    inputChange(e) {
      this.setData({
        orgName: e.detail,
      })
      clearTimeout(this.data.timer)
      this.data.timer = setTimeout(() => {
        this.getInitDataList()
      }, 500)
    },

    itemClick(e) {
      let type = e.type
      let id = ''
      if (type == 'change') {
        id == e.detail
      } else {
        id = e.target.dataset.id
      }
      let {
        dataList
      } = this.data
      let orgItemInfo = {}
      dataList.forEach(item => {
        if (item.id === id) {
          orgItemInfo = item
        }
      })
      this.setData({
        orgItemInfo
      })
    },
    itemClick1(e) {
      let {
        item
      } = e.currentTarget.dataset
      this.setData({
        orgItemInfo: item,
        selectId: item.id,
      })
    },

    confirm() {
      this.triggerEvent('confirmUnitSelection', {
        showUnitSelection: false,
        msg: true,
        orgInfo: this.data.orgItemInfo
      });
    },
    onClose() {
      this.triggerEvent('confirmUnitSelection', {
        showUnitSelection: false,
        msg: false,
      });
    },

    conReset() {
      this.setData({
        "orgItemInfo.id": '',
        "orgItemInfo.orgId": '',
        "orgItemInfo.orgName": '',
        "orgItemInfo.type": '',

      })
      this.triggerEvent('confirmUnitSelection', {
        showUnitSelection: false,
        msg: true,
        orgInfo: this.data.orgItemInfo
      });
    },

    // 点击复制编号
    clickCopy(e) {
      let {
        code
      } = e.currentTarget.dataset
      wx.setClipboardData({
        data: code,
        success(res) {
          wx.getClipboardData({
            success(res) {
              showMsg(`编号 ${code} 已复制`)
            }
          })
        }
      })
    },
  }
})